using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace SpriteLib
{
    public class SpriteText: ISprite
    {
        public event EventHandler<SpriteEffectArg> OnStart;
        public event EventHandler<SpriteEffectArg> OnEnd;

        private List<ISprite> childSprites = new List<ISprite>();
        private List<ISpriteEffect> spriteEffects = new List<ISpriteEffect>();

        public Vector2 Position
        {
            get;
            set;
        }

        public SpriteFont SpriteFont
        {
            get;
            set;
        }

        public float Orientation
        {
            get;
            set;
        }

        public Color Color
        {
            get;
            set;
        }

        public String Text
        {
            get;
            set;
        }

        public float Scale
        {
            get;
            set;
        }

        public SpriteText()
        {
            Scale = 1.0f;
        }
 
        public void Draw(SpriteBatch spriteBatch) 
        {
            Vector2 centerString = SpriteFont.MeasureString(Text)/2.0f;
            spriteBatch.DrawString(
                SpriteFont, 
                Text, 
                Position, 
                Color, 
                Orientation,
                centerString,
                Scale,
                SpriteEffects.None,
                0);

            foreach (var sprite in childSprites)
            {
                sprite.Draw(spriteBatch);
            }
        }

        public void AddChildSprite(ISprite sprite) 
        {
            childSprites.Add(sprite);
        }

        public void AttachSpriteEffect(ISpriteEffect spriteEffect)
        {
            spriteEffect.Sprite = this;
            spriteEffects.Add(spriteEffect);
            spriteEffect.OnStart += (_sender, _args)
                =>
            {
                if (OnStart != null)
                    OnStart(this, new SpriteEffectArg(_sender as ISpriteEffect));
            };
            spriteEffect.OnEnd += (_sender, _args)
                =>
            {
                if (OnEnd != null)
                    OnEnd(this, new SpriteEffectArg(_sender as ISpriteEffect));
            };
        }



        public void DetachSpriteEffect(ISpriteEffect spriteEffect)
        {
            spriteEffects.Remove(spriteEffect);
        }

        public void Update(GameTime gameTime)
        {
            foreach (var effect in spriteEffects)
            {
                effect.Update(gameTime);
            }
        }

        public void RemoveAllSpriteEffects()
        {
            spriteEffects.Clear();
        }

    }
}
